home *** CD-ROM | disk | FTP | other *** search
/ EDUCORP 8 / Educorp2Compilation.sit / educorp2 / Programming (2300, 7000) / 2309 Programming v.7 / Help Demo / Help.p next >
Encoding:
Text File  |  1989-01-29  |  5.5 KB  |  221 lines

  1. unit HelpUnit;
  2.  
  3. interface
  4.  
  5.     uses
  6.         QuickDraw, ToolIntf;
  7.     procedure Help (strRsrcNum, hlpRsrcNum: Integer);
  8.  
  9. implementation
  10.  
  11.     type
  12.         TopicSet = record
  13.                 textRsrc, stylRsrc: Integer;
  14.             end;
  15.         HelpListHdl = ^HelpListPtr;
  16.         HelpListPtr = ^HelpList;
  17.         HelpList = record
  18.                 numTopics: Integer;
  19.                 topicSets: array[0..0] of TopicSet;
  20.             end;
  21.     var
  22.         myDialog: DialogPtr;
  23.         myHelpListH: HelpListHdl;
  24.         theList: ListHandle;
  25.         pagesize, numTopics: Integer;
  26.         textRes, numTopicsH: Handle;
  27.         sBar: ControlHandle;
  28.         theText: TEHandle;
  29.  
  30.     procedure CreateList;
  31.         var
  32.             i, iType: Integer;
  33.             topicStr: Str255;
  34.             iRect, dataBounds: Rect;
  35.             cSize, theCell: Cell;
  36.             iHandle: Handle;
  37.     begin
  38.         GetDItem(myDialog, 3, iType, iHandle, iRect);
  39.         FrameRect(iRect);
  40.         InsetRect(iRect, 1, 1);
  41.         SetPt(cSize, iRect.right - iRect.left, 17);
  42.         SetRect(dataBounds, 0, 0, 1, numTopics);
  43.         theList := LNew(iRect, dataBounds, cSize, 0, myDialog, False, False, False, True);
  44.         for i := 1 to numTopics do                    {drawIt, hasGrow, hScroll, vScroll}
  45.             begin
  46.                 GetIndString(topicStr, 256, i);
  47.                 SetPt(theCell, 0, i - 1);
  48.                 LSetCell(Pointer(Ord(@topicStr) + 1), Length(topicStr), theCell, theList);
  49.             end;
  50.         theList^^.selFlags := lOnlyOne;
  51.         SetPt(theCell, 0, 0);
  52.         LSetSelect(true, theCell, theList);
  53.         LDoDraw(True, theList); {set drawing to True now that list is built}
  54.     end;
  55.  
  56.     procedure ScrAction (theCtl: ControlHandle; partCode: Integer);
  57.         var
  58.             delta, oldValue: Integer;
  59.     begin
  60.         case partCode of
  61.             inUpButton: 
  62.                 delta := -10;
  63.             inDownButton: 
  64.                 delta := 10;
  65.             inPageUp: 
  66.                 delta := -pagesize;
  67.             inPageDown: 
  68.                 delta := pagesize;
  69.             otherwise
  70.         end;
  71.         if partCode <> 0 then
  72.             begin
  73.                 oldValue := GetCtlValue(theCtl);
  74.                 SetCtlValue(theCtl, oldValue + delta);
  75.                 TEScroll(0, oldValue - GetCtlValue(theCtl), theText);
  76.             end;
  77.     end;
  78.  
  79.     procedure WindowScroll (thePt: Point; theWindow: WindowPtr);
  80.         var
  81.             theCtl: ControlHandle;
  82.             part, oldValue: Integer;
  83.     begin
  84.         GlobalToLocal(thePt);
  85.         case FindControl(thePt, theWindow, theCtl) of
  86.             inUpButton..inPageDown: 
  87.                 part := TrackControl(theCtl, thePt, @ScrAction);
  88.             inThumb: 
  89.                 begin
  90.                     with theText^^ do
  91.                         oldValue := viewRect.top - destRect.top;
  92.                     if TrackControl(theCtl, thePt, nil) <> 0 then
  93.                         TEScroll(0, oldValue - GetCtlValue(theCtl), theText);
  94.                 end;
  95.             otherwise
  96.         end;
  97.     end;
  98.  
  99.     procedure LoadText (item: Integer);
  100.         const
  101.             inactive = 255;
  102.         var
  103.             max, textNum, stylNum: Integer;
  104.             textRes, stylRes: Handle;
  105.     begin
  106.         TEDeactivate(theText);
  107.         TESetSelect(0, 32767, theText);
  108.         TEDelete(theText);
  109.         theText^^.destRect := theText^^.viewRect;
  110.         textNum := myHelpListH^^.topicSets[item].textRsrc;
  111.         stylNum := myHelpListH^^.topicSets[item].stylRsrc;
  112.         if (textNum <> 0) & (stylNum <> 0) then
  113.             begin
  114.                 textRes := GetResource('TEXT', textNum);
  115.                 stylRes := GetResource('styl', stylNum);
  116.                 HLock(textRes);
  117.                 HLock(stylRes);
  118.                 TEStylInsert(textRes^, SizeResource(textRes), StScrpHandle(stylRes), theText);
  119.                 HUnlock(stylRes);
  120.                 HUnlock(textRes);
  121.             end;
  122.         max := TEGetHeight(theText^^.nLines, 0, theText) - pagesize;
  123.         if max > 0 then
  124.             HiliteControl(sBar, activeFlag)
  125.         else
  126.             HiliteControl(sBar, inactive);
  127.         SetCtlValue(sBar, 0);
  128.         SetCtlMax(sBar, max);
  129.         TEActivate(theText);
  130.     end;
  131.  
  132.     function MyFilter (theDialog: DialogPtr; var theEvent: EventRecord; var itemHit: Integer): Boolean;
  133.         var
  134.             iType, part: Integer;
  135.             iBox: Rect;
  136.             iHdl: Handle;
  137.             thePt: Point;
  138.             theCtl: ControlHandle;
  139.             ignore: Boolean;
  140.             newCell: Cell;
  141.     begin
  142.         MyFilter := False;
  143.         case theEvent.what of
  144.             keyDown, autoKey:  {close if enter or return is keyed}
  145.                 if BitAnd(theEvent.message, charCodeMask) in [3, 13] then
  146.                     begin
  147.                         MyFilter := True;
  148.                         GetDItem(theDialog, 1, iType, iHdl, iBox);
  149.                         InvertRoundRect(iBox, 10, 10);
  150.                         itemHit := 1;
  151.                     end;
  152.             mouseDown: 
  153.                 begin
  154.                     thePt := theEvent.where;
  155.                     GlobalToLocal(thePt);
  156.                     part := FindControl(thePt, myDialog, theCtl);
  157.                     if theCtl = sBar then
  158.                         WindowScroll(theEvent.where, myDialog)
  159.                     else if theCtl = theList^^.vScroll then
  160.                         ignore := LClick(thePt, theEvent.modifiers, theList)
  161.                     else if PtInRect(thePt, theList^^.rView) then
  162.                         begin
  163.                             ignore := LClick(thePt, theEvent.modifiers, theList);
  164.                             SetPt(newCell, 0, 0);
  165.                             ignore := LGetSelect(True, newCell, theList);
  166.                             LoadText(newCell.v);
  167.                         end;
  168.                 end;
  169.             updateEvt: 
  170.                 begin
  171.                     TEUpdate(theText^^.viewRect, theText);
  172.                     LUpdate(myDialog^.visRgn, theList);
  173.                 end;
  174.             otherwise
  175.         end;
  176.     end;
  177.  
  178.     procedure Help;  {(strRsrcNum, hlpRsrcNum: Integer)}
  179.         type
  180.             IntHdl = ^IntPtr;
  181.             IntPtr = ^Integer;
  182.         var
  183.             savePort: GrafPtr;
  184.             iBox, tBox: Rect;
  185.             iType, itemHit: Integer;
  186.             iHdl: Handle;
  187.     begin
  188.         GetPort(savePort);
  189.         myDialog := GetNewDialog(1001, nil, Pointer(-1));
  190.         SetPort(myDialog);
  191.         ShowWindow(myDialog);
  192.         TextFont(Geneva);
  193.         TextSize(12);
  194.         numTopics := IntHdl(GetResource('STR#', strRsrcNum))^^;
  195.         myHelpListH := HelpListHdl(GetResource('HLP#', hlpRsrcNum));
  196.         HLock(Handle(myHelpListH));
  197.         CreateList;
  198.         GetDItem(myDialog, 2, iType, iHdl, tBox);
  199.         FrameRect(tBox);
  200.         InsetRect(tBox, 4, 2);
  201.         pagesize := tBox.bottom - tBox.top;
  202.         theText := TEStylNew(tBox, tBox);
  203.         HLock(Handle(theText));
  204.         GetDItem(myDialog, 6, iType, iHdl, iBox);
  205.         sBar := ControlHandle(iHdl);
  206.         GetDItem(myDialog, 1, iType, iHdl, iBox);
  207.         InsetRect(iBox, -4, -4);
  208.         PenSize(3, 3);
  209.         FrameRoundRect(iBox, 16, 16);
  210.         PenSize(1, 1);
  211.         LoadText(0);
  212.         repeat
  213.             ModalDialog(@MyFilter, itemHit);
  214.         until itemHit = 1;
  215.         HUnlock(Handle(theText));
  216.         LDispose(theList);
  217.         DisposDialog(myDialog);
  218.         SetPort(savePort);
  219.     end;
  220.  
  221. end.